package com.hust.domain;

import lombok.Data;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Data
public class PredictResult {

    private List<Date> t;

    private List<Double> TP;

    private List<Double> NTU;

    private List<Double> IMn;


    public void FromXlsx(String filename) {

        try {
            //获取文件流
            FileInputStream inputStream = new FileInputStream(filename);
            //1.创建工作簿,使用excel能操作的这边都看看操作
            Workbook workbook = new XSSFWorkbook(inputStream);
            //读取工作簿1：主成分信息
            Sheet sheet = workbook.getSheetAt(0);
            String[] names = {"t", "TP", "NTU", "IMn"};
            int headRow = 1;
            int headCol = 1;
            for (int i = headCol; i <= sheet.getRow(0).getLastCellNum(); i++) {
                List<Double> data = new ArrayList<>();
                for (int j = headRow; j < sheet.getLastRowNum(); j++) {
                    Row row = sheet.getRow(j);
                    Cell cell = row.getCell(i);
                    data.add(cell.getCellType() == CellType.NUMERIC ?
                            cell.getNumericCellValue() : Double.valueOf(cell.getStringCellValue()));
                }
                Field field = this.getClass().getDeclaredField(names[i]);
                field.setAccessible(true); // 使私有字段可访问
                field.set(this, data);
            }

            inputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}
